package com.gallop.mq;

import com.gallop.mq.core.CodecEnum;
import com.gallop.mq.core.SupportedType;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;

@Getter
@Setter
@ToString
@ConfigurationProperties("gallop.mq")
public class MQProperties {

    /**
     * MQ集群地址，[scheme://]ip:port 使用,分隔
     */
    private List<String> hosts;
    /**
     * 连接MQ用户名
     */
    private String user;
    /**
     * 连接MQ密码
     */
    private String secret;
    /**
     * MQ类型
     */
    private SupportedType type;
    /**
     * 消息编解码器
     */
    private CodecEnum codec;
    /**
     * RPC超时时间（毫秒），设置小于0时一直等待无超时
     */
    private Integer requestTimeout = 15000;
    /**
     * 断线重连间隔（毫秒）
     */
    private Integer reconnectInterval = 5000;

    // TODO using map
    // custom config
    private RabbitMQConfig rabbitmq;

    private KafkaMQConfig kafka;

    private ActiveMQConfig amq;

    private NatsMQConfig nats;

    @Data
    public static class RabbitMQConfig {

        /**
         * 本地默认的exchange名称
         */
        private String exchange = "";

        /**
         * 本地默认的rpc模式接收队列名称
         */
        private String rpcReplyQueue = "";
        private String virtualHost = "/";
    }

    @Data
    public static class KafkaMQConfig {
        /**
         * 拉取监听消息时间间隔（毫秒）
         */
        private Integer msgPollInterval = 100;
        /**
         * 拉取监听消息等待时长（毫秒）
         */
        private Integer msgPollTimeout = 200;
        /**
         * topic监听默认group
         */
        private String defaultTopicGroup = "def";
    }

    @Data
    public static class ActiveMQConfig {

        /**
         * 收发MQ消息使用字节缓存大小，默认256
         */
        private Integer ioBufferSize = 256;
    }

    @Data
    public static class NatsMQConfig {
        private int maxMessagesInOutgoingQueueSize;
    }

    public RabbitMQConfig getRabbitmq() {
        if (rabbitmq == null) {
            rabbitmq = new RabbitMQConfig();
        }
        return rabbitmq;
    }

    public KafkaMQConfig getKafka() {
        if (kafka == null) {
            kafka = new KafkaMQConfig();
        }
        return kafka;
    }

    public ActiveMQConfig getAmq() {
        if (amq == null) {
            amq = new ActiveMQConfig();
        }
        return amq;
    }

    public NatsMQConfig getNats() {
        if (nats == null) {
            nats = new NatsMQConfig();
        }
        return nats;
    }
}
